/**
 * Author: Lily Jiang
 * Create Time: 2019/7/10
 * Description: 实现可多次被触发的bus.once，不用bus.on 是因为页面卸载之还会触发on的回调
 * todo: 绑定多个事件，还没测过
 */

let bus = {
  data () {
    return {
      busFlag: true
    }
  },
  methods: {
    busOnce (handlerName, callback) {
      if (!handlerName) {
        return
      }

      this.$bus.once(handlerName, () => {
        if (!this.busFlag) {
          return
        }

        callback && callback()
        this.busOnce(handlerName, callback)
      })
    },
    busOff () {
      this.busFlag = false
    }
  },
  // beforeDestroy () {
  //   this.busOff()
  // },
  onUnload () {
    this.busOff()
  }
}

export default bus
